package dao.daoimpl;

import util.DataBase;
import dao.CommentDao;
import bean.Comment;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CommentDaoImpl implements CommentDao {
    @Override
    public boolean addComment(Comment comment) {
        String content=comment.getContent();
        String time=comment.getTime();
        int articleId=comment.getArticleId();
        int fatherId=comment.getFatherId();
        String commenter=comment.getCommenter();
        String headPortrait=comment.getHeadPortrait();
        String sql="insert into comment_table (comment_fatherId,comment_content,comment_time,comment_articleId,comment_commenter,comment_headPortrait) values('"+fatherId+"','"+content+"','"+time+"','"+articleId+"','"+commenter+"','"+headPortrait+"')";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<Comment> getComments(int articleId) throws SQLException {
        ArrayList<Comment> comments=new ArrayList<>();
        String sql="select * from comment_table where comment_articleId='"+articleId+"'";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            int cId=resultSet.getInt("comment_id");
            int fId=resultSet.getInt("comment_fatherId");
            String content=resultSet.getString("comment_content");
            String time=resultSet.getString("comment_time");
            int aId=resultSet.getInt("comment_articleId");
            String commenter=resultSet.getString("comment_commenter");
            String headPortrait=resultSet.getString("comment_headPortrait");
            comments.add(new Comment(cId,fId,content,time,aId,commenter,headPortrait));
        }
        DataBase.release();
        return comments;
    }
}
